import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:get/get.dart';
import 'package:toatool/src/data/remote/app_exceptions.dart';
import 'view_state_list_controller.dart';

/// 基于ViewStateListController
abstract class ViewStateRefreshListController<T>
    extends ViewStateListController<T> {
  /// 分页第一页页码
  static const int pageNumFirst = 1;

  /// 分页条目数量
  static const int size = 6;

  late dynamic arguments;

  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  RefreshController get refreshController => _refreshController;

  /// 当前页码
  int currentPageNum = pageNumFirst;

  /// 下拉刷新
  /// [init] 是否是第一次加载
  /// true:  Error时,显示错误页面信息
  /// false: Error时,显示原页面,弹出错误
  @override
  Future<List<T>?> mitRefresh(
      {bool init = false, bool requiredAuth = true}) async {
    try {
      currentPageNum = pageNumFirst;
      var data = await loadData(pageNumFirst, size);
      if (data.isEmpty) {
        refreshController.refreshCompleted(resetFooterState: true);
        list.clear();
        setEmpty();
      } else {
        onCompleted(data);
        list.clear();
        list.addAll(data);
        refreshController.refreshCompleted();
        // 小于分页的数量,禁止上拉加载更多
        if (data.length < size) {
          refreshController.loadNoData();
        } else {
          //防止上次上拉加载更多失败,需要重置状态
          refreshController.loadComplete();
        }
        setIdle();
      }
      update();
      return data;
    } on AppException catch (e, s) {
      /// 页面已经加载了数据,如果刷新报错,不应该直接跳转错误页面
      /// 而是显示之前的页面数据.给出错误提示
      if (init) {
        list.clear();
      }
      setError(e, s, message: e.message);
      update();
      refreshController.refreshFailed();
      return null;
    } catch (e, s) {
      if (init) {
        list.clear();
      }
      setError(e, s, message: 'データ異常');
      update();
      refreshController.refreshFailed();
      Get.snackbar<void>('エラー', 'データ異常');
      return null;
    }
  }

  /// 上拉加载更多
  Future<List<T>?> loadMore() async {
    try {
      var data = await loadData(++currentPageNum, size);
      if (data.isEmpty) {
        currentPageNum--;
        refreshController.loadNoData();
      } else {
        onCompleted(data);
        list.addAll(data);
        if (data.length < size) {
          refreshController.loadNoData();
        } else {
          refreshController.loadComplete();
        }
        update();
      }
      return data;
    } on AppException catch (e, s) {
      currentPageNum--;
      refreshController.loadFailed();
      debugPrint('error--->\n' + e.toString());
      debugPrint('statck--->\n' + s.toString());
      setError(e, s, message: e.message);
      update();
      return null;
    } catch (e, s) {
      // 没有指定的类型，处理所有异常
      currentPageNum--;
      refreshController.refreshFailed();
      print('Something really unknown: $e');
      setError(e, s, message: '出错了');
      update();
      return null;
    }
  }

  // 加载数据
  @override
  Future<List<T>> loadData([int current, int size]);

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }
}
